﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.Mathd;
namespace BridgeShips
{
    public class CaculateLonAndLatiData
    {



        /*
        *                                 /   /                f0                                 f1
        *                                f0  f1  (m_TopRightPoint)m_TopRightSai   (m_BottomRightFixSai)m_TopRightSai   
        *                               /   /     
        *       (m_TopLeftPoint)       f2  / 
        *       m_BottomRightPoint    /   /    
        */


        private Vector2d m_vec2dImportmentPos = Vector2d.zero;
        private Vector2d m_vec2dFirstPos = Vector2d.zero;
        private Vector2d m_vec2dSecondPos = Vector2d.zero;
        // Use this for initialization 
        // 求河的方向

        private float m_riverAngle = 0.0f;


        public  CaculateLonAndLatiData(Vector2d vec2dImporPos,
            Vector2d vec2dFirstPos, Vector2d vec2dSecondPos)
        {
            m_vec2dImportmentPos = vec2dImporPos;
            m_vec2dFirstPos = vec2dFirstPos;
            m_vec2dSecondPos = vec2dSecondPos;

           // LongtiAndLatitudeAngle();
        }
        public float RiverDirection
        {
            get { return m_riverAngle; }
        }
        private void LongtiAndLatitudeAngle()
        {
            double lastAngle = GetLineAngle(m_vec2dImportmentPos,m_vec2dFirstPos);
            double thisAngle = GetLineAngle(m_vec2dImportmentPos, m_vec2dSecondPos);

            m_riverAngle = Mathf.Abs((float)(thisAngle - lastAngle));
            if (m_riverAngle > 180.0f)
            {
                m_riverAngle = 360.0f - m_riverAngle;
            }
            m_riverAngle =180.0f -  m_riverAngle;
        }

        private double GetLineAngle(Vector2d startPoint, Vector2d endPoint)
        {
            double mathPI = 3.1415926535897931;
            double tmpValue = 0;
            double latStart = startPoint.y * mathPI / 180;
            double lngStart = startPoint.x * mathPI / 180;
            double latEnd = endPoint.y * mathPI / 180;
            double lngEnd = endPoint.x * mathPI / 180;
            if (startPoint.x == endPoint.x || startPoint.y == endPoint.y)
            {
                if (startPoint.x == endPoint.x)
                {
                    /// 经度相同
                    if (endPoint.y >= startPoint.y)
                    {
                        return 0;
                    }
                    else
                    {
                        return 180;
                    }
                }
                else
                {
                    /// 纬度相同
                    if (endPoint.x >= startPoint.x)
                    {
                        return 90;
                    }
                    else
                    {
                        return 270;
                    }
                }
            }

            tmpValue = Mathf.Sin((float)latStart) * Mathf.Sin((float)latEnd) + Mathf.Cos((float)latStart) * Mathf.Cos((float)latEnd) * Mathf.Cos((float)(lngEnd - lngStart));
            tmpValue = Mathf.Sqrt((float)(1.0f - tmpValue * tmpValue));
            tmpValue = Mathf.Cos((float)latEnd) * Mathf.Sin((float)(lngEnd - lngStart)) / tmpValue;
            double resultAngle = Mathf.Abs(Mathf.Asin((float)tmpValue) * 180 / (float)mathPI);

            if (endPoint.x > startPoint.x)
            {
                if (endPoint.y >= startPoint.y)
                {
                    /// 第一象限 
                    return resultAngle;
                }
                else
                {
                    /// 第二象限 
                    return 180 - resultAngle;
                }
            }
            else
            {
                /// 第四象限
                if (endPoint.y >= startPoint.y)
                {
                    return 360 - resultAngle;
                }
                else
                {
                    /// 第三象限
                    return 180 + resultAngle;
                }
            }
        }

        //通过经纬度获取河的沿河边校准点长度
        public double GetHorizontalDistance()
        {

           double value1 =  GetDistance(m_vec2dImportmentPos.y, m_vec2dImportmentPos.x,
                31.862117, 118.5551762);
            Debug.LogError("value1 + " + value1);
            double value2 = GetVerticalDistance();
            Debug.LogError("value2 + " + value2);

            return GetDistance(m_vec2dImportmentPos.y, m_vec2dImportmentPos.x,
                m_vec2dSecondPos.y, m_vec2dSecondPos.x );
        }

        //通过经纬度获取河的沿河边校准点宽度
        public double GetVerticalDistance()
        {
            double testValue =  GetDistance( m_vec2dFirstPos.y, m_vec2dFirstPos.x,
                 m_vec2dImportmentPos.y, m_vec2dImportmentPos.x );

          //  Debug.LogError("testValue 000 == " + testValue);


            return testValue;
        }


        public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
        {
            double radLat1 = rad(lat1);

            double radLat2 = rad(lat2);

            double a = radLat1 - radLat2;

            double b = rad(lng1) - rad(lng2);

            double s = 2 * Mathf.Asin(Mathf.Sqrt(Mathf.Pow(Mathf.Sin((float)a / 2), 2) +
                Mathf.Cos((float)radLat1) * Mathf.Cos((float)radLat2) * Mathf.Pow(Mathf.Sin((float)b / 2), 2)));

            s = s * EARTH_RADIUS;

            s = Mathf.Round((float)s * 10000) / 10;
            return s;
        }

        private static double rad(double d)
        {
            return d * Mathf.PI / 180.0;
        }

        private const double EARTH_RADIUS = 6371.379;


        ///////////////////////////////////////////////////////////////////////////////////////
    }

}